from App.exts import db
from dataclasses import dataclass


@dataclass
class NewsCategory(db.Model):
    __tablename__ = 'news_category'

    id: int = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='新闻类型ID')
    label: str = db.Column(db.String(50), nullable=False, unique=True, comment='新闻类型标签')
    value: str = db.Column(db.String(100), nullable=False, comment='新闻类型值')
    create_time: object = db.Column(db.DateTime, default=db.func.current_timestamp(), comment='创建时间')
    update_time: object = db.Column(db.DateTime, default=db.func.current_timestamp(),
                                    onupdate=db.func.current_timestamp(),
                                    comment='最后修改时间')

    def __eq__(self, other):
        if isinstance(other, NewsCategory):
            return self.id == other.id
        return False

    def __repr__(self):
        return f'NewsCategory[id={self.id}, label={self.label}, value={self.value}, create_time={self.create_time}, update_time={self.update_time}]'


class News(db.Model):
    __tablename__ = 'news'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='新闻ID')
    third_id = db.Column(db.String(50), unique=True, nullable=False, comment='第三方网站ID')
    third_source = db.Column(db.String(50), nullable=False, comment='新闻来源')
    title = db.Column(db.String(255), nullable=False, comment='新闻标题')
    author = db.Column(db.String(100), nullable=False, comment='作者')
    cover_image = db.Column(db.String(255), comment='封面图片路径')
    comment_count = db.Column(db.Integer, default=0, nullable=False, comment='评论数')
    publish_date = db.Column(db.Date, nullable=False, comment='发布日期')
    detail_link = db.Column(db.String(255), nullable=False, comment='详情链接')
    category_id = db.Column(db.Integer, db.ForeignKey('news_category.id'), nullable=False, comment='新闻类型ID')
    create_time = db.Column(db.DateTime, default=db.func.current_timestamp(), comment='创建时间')
    update_time = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp(),
                            comment='最后修改时间')

    # 定义与NewsCategory模型的关系
    category = db.relationship('NewsCategory', backref=db.backref('news', lazy=True))

    # def __init__(self, third_id, title, author, publish_date, detail_link, category_id, cover_image=None):
    #     self.third_id = third_id
    #     self.title = title
    #     self.author = author
    #     self.publish_date = publish_date
    #     self.detail_link = detail_link
    #     self.category_id = category_id
    #     self.cover_image = cover_image

    def __repr__(self):
        return f'News[id={self.id}, third_id={self.third_id}, title={self.title}, author={self.author}, publish_date={self.publish_date}, detail_link={self.detail_link}, category_id={self.category_id}, create_time={self.create_time}, update_time={self.update_time}]'

    def __eq__(self, other):
        if isinstance(other, News):
            return self.id == other.id
        return False


class NewsDetails(db.Model):
    __tablename__ = 'news_details'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='详情ID')
    third_id = db.Column(db.String(50), db.ForeignKey('news.third_id'), nullable=False, comment='第三方网站ID')
    detail = db.Column(db.Text, nullable=False, comment='文章详情')
    like_count = db.Column(db.Integer, default=0, nullable=False, comment='点赞数')
    detailed_publish_time = db.Column(db.DateTime, nullable=False, comment='详细发布时间')
    create_time = db.Column(db.DateTime, default=db.func.current_timestamp(), comment='创建时间')
    update_time = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp(),
                            comment='最后修改时间')

    # 定义与News模型的关系
    news = db.relationship('News', backref=db.backref('details', uselist=False))

    def __init__(self, third_id, detail, detailed_publish_time):
        self.third_id = third_id
        self.detail = detail
        self.detailed_publish_time = detailed_publish_time

    def __repr__(self):
        return f'NewsDetails[id={self.id}, third_id={self.third_id}, detail={self.detail}, like_count={self.like_count}, detailed_publish_time={self.detailed_publish_time}, create_time={self.create_time}, update_time={self.update_time}]'

    def __eq__(self, other):
        if isinstance(other, NewsDetails):
            return self.id == other.id
        return False
